package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

/**
 * 鼠标右键菜单
 * @author rebin
 *
 */
public class MousRightMenu extends NokaTag{

	private static final long serialVersionUID = -4559582840999409303L;
	private String id = null;//id
	private String width = "200px";//宽度
	private String style = "display:none;z-index:99;";
	private String classStyle="";
	private String autohide = null;//自动隐藏
	private Integer menuid=0;
	public String getMenuItemID(){
		menuid+=1;
		return id+"_DIV_"+menuid;
	}
	public int doStartTag() throws JspException {
		menuid=0;
		JspWriter jspOut = pageContext.getOut();
		StringBuilder body = new StringBuilder();
		body.append("<div id=\""+id+"\" class=\""+classStyle+"\" style=\"width:"+LenFormat(width)+";"+style+"\">");
		try {
			jspOut.write(body.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		try {
				StringBuilder body = new StringBuilder();
				HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
				body.append("</div>");
				body.append(potjs(request));//js框架
				body.append(MousRigthMenuJS(request));//MousRigthMenuJS js
				body.append("<script type=\"text/javascript\">");
				body.append("Event.observe(document, 'dom:loaded', function() {");
				body.append("document.oncontextmenu=function(){return false;};");
				if("false".equalsIgnoreCase(autohide)){
					body.append("var nk"+id+"Menu = new NKMousRightMenu('"+id+"',"+menuid+",true);");
				}else
					body.append("var nk"+id+"Menu = new NKMousRightMenu('"+id+"',"+menuid+");");
				body.append("nk"+id+"Menu.show();");
				body.append("});");
				body.append("Event.observe(document,'contextmenu',function(event){");
				body.append("Event.stop(event);");
				body.append("$('"+id+"').nshow(Event.pointerX(event),Event.pointerY(event));");
				body.append("});");
				body.append("</script>");
				pageContext.getOut().write(body.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return EVAL_PAGE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getClassStyle() {
		return classStyle;
	}

	public void setClassStyle(String classStyle) {
		this.classStyle = classStyle;
	}
	public String getAutohide() {
		return autohide;
	}
	public void setAutohide(String autohide) {
		this.autohide = autohide;
	}
}
